package com.jm.weibo.ui;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import weibo4andriod.User;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jm.weibo.logic.AttentionAdapter;
import com.jm.weibo.logic.IWeiboActivity;
import com.jm.weibo.logic.MainService;
import com.jm.weibo.logic.Task;

public class ListAllFriendsActivity extends Activity implements IWeiboActivity{
	
	public static final String REF_ATTENTION="REF_ATTENTION";//更新关注
	
	public static final String REF_MORE_ATTENTION="REF_MORE_ATTENTION";//更新更多关注
	
	public static final String REF_ICON="REF_ICON";//更新更多关注
	
	private View vTitle;
	
	private Button btnReturn;

	private Button btnHome;
	
	private TextView tvTitle;
	
	private ListView listView;
	
	private View progress;
	
	private int nowpage4Attention = 1;
	
	private int pagesize4Attention = 15;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.list_all_friends);
		this.initControlsAndAddControlsEvent();
		MainService.mainService.allActivity.add(this);
		
		Map taskParam = new HashMap();
		taskParam.put("nowpage4Attention", nowpage4Attention);
		taskParam.put("pagesize4Attention", pagesize4Attention);
		Task task = new Task(Task.TAST_GET_ATTENTION_INFO, taskParam);
		MainService.mainService.newTask(task);
	}
	
	private void initControlsAndAddControlsEvent() {
		 this.vTitle = this.findViewById(R.id.title4ListAllFriends);
		 this.tvTitle = (TextView) this.vTitle.findViewById(R.id.textView);
		 this.btnReturn = (Button) this.vTitle.findViewById(R.id.title_bt_left);
		 this.btnHome = (Button) this.vTitle.findViewById(R.id.title_bt_right);
         this.listView = (ListView) this.findViewById(R.id.listAllFriends);
		 this.progress = this.findViewById(R.id.progress4ListAllFriends);
		 
         
         this.btnReturn.setText(R.string.imageviewer_back);
		 this.btnHome.setBackgroundResource(R.drawable.title_home);
		 this.tvTitle.setText(R.string.attend);
		 
		 this.listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if(arg3==-1){   //更多
					Toast.makeText(ListAllFriendsActivity.this, "正在获取跟多的关注信息", Toast.LENGTH_SHORT).show();
					nowpage4Attention++;
				    Map taskParam = new HashMap();
					taskParam.put("pagesize4Attention", ListAllFriendsActivity.this.pagesize4Attention);
					taskParam.put("nowpage4Attention", ListAllFriendsActivity.this.nowpage4Attention);
					Task tsAttention=new Task(Task.TAST_GET_MORE_ATTENTION_INFO, taskParam);
					MainService.addNewTask(tsAttention);
				
				}else if(arg3==0){  //刷新
					
					Toast.makeText(ListAllFriendsActivity.this, "刷新", Toast.LENGTH_SHORT).show();
					HashMap taskParam = new HashMap();
					taskParam.put("pagesize4Attention", ListAllFriendsActivity.this.pagesize4Attention);
					taskParam.put("nowpage4Attention", ListAllFriendsActivity.this.nowpage4Attention);
			    	Task tsAttention=new Task(Task.TAST_GET_ATTENTION_INFO, taskParam);
			    	MainService.addNewTask(tsAttention);
				
				}else{
					Toast.makeText(ListAllFriendsActivity.this, "查看给好友的信息", Toast.LENGTH_SHORT).show();
//				    Intent it=new Intent(MyListActivity.this,ViewWeiboActivity.class);
//				    try{
//					    StatusAdapter ma=(StatusAdapter)lv.getAdapter();
//					    Status statu=(Status)ma.allstatus.get(arg2-1);
//					    it.putExtra("status",statu);
//				    }catch(Exception e){
//				    	Log.e("e","error"+e);
//				    }
////				    
//				    MyListActivity.this.startActivityForResult(it, 0);
				}
				
		 }
		});
		 
	   //添加返回事件 
	   this.btnReturn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ListAllFriendsActivity.this.finish();
			}
		});
	   
	  this.btnHome.setOnClickListener(new OnClickListener() {
		
		 @Override
		 public void onClick(View v) {
			Intent intent = new Intent(ListAllFriendsActivity.this, MyListActivity.class);
			ListAllFriendsActivity.this.startActivity(intent);
		 }
	  });
	}
	@Override
	public void init() {
		 
		
	}

	@Override
	public void refresh(Object... param) {
		Log.d("ref", (String)param[0]);
		String flag = (String)param[0];
		if(flag.equals(REF_ATTENTION)){
			
			List<User> friends=(List<User>)param[1];
			AttentionAdapter adapter=new AttentionAdapter(this,friends);
			this.listView.setAdapter(adapter);//更新列表
		    this.progress.setVisibility(View.GONE);
		    
		}else if(flag.equals(REF_MORE_ATTENTION)){ //更多
			List<User> friends=(List<User>)param[1];
			((AttentionAdapter)this.listView.getAdapter()).addMoreData(friends);
		    this.progress.setVisibility(View.GONE);
		}else if(flag.equals(REF_ICON)){
			//让数据适配器getView方法重新调用
			((AttentionAdapter)this.listView.getAdapter()).notifyDataSetChanged();
		}
		
	}

}
